package com.jichaoyun.service.util.ComUtils;

import com.jichaoyun.service.impl.ComPortServiceImpl;
import gnu.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
import java.util.TooManyListenersException;


public class SerialPortUtil {

    private InputStream in = null;

    private  int readLen = 0;

    private Float money = 0f;

    private static final Logger logger = LoggerFactory.getLogger(SerialPortUtil.class);


    /**
     * 关闭串口
     *
     * @param serialPort 待关闭的串口对象
     */
    public  void closePort(SerialPort serialPort) {
        if (serialPort != null) {
            serialPort.close();
            System.out.println("close port successfully!");
            serialPort = null;
        }
    }

    /**
     * 往串口发送数据
     *
     * @param serialPort 串口对象
     * @param sendArray      待发送数据
    //     * @throws SendDataToSerialPortFailure        向串口发送数据失败
    //     * @throws SerialPortOutputStreamCloseFailure 关闭串口对象的输出流出错
     */
    public  void sendToPort(SerialPort serialPort, byte[] sendArray)  {
        OutputStream out = null;
        try {
            out = serialPort.getOutputStream();
            out.write(sendArray);
            out.flush();
        } catch (IOException e) {
//            throw new SendDataToSerialPortFailure();
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                    out = null;
                }
            } catch (IOException e) {
//                throw new SerialPortOutputStreamCloseFailure();
                e.printStackTrace();
            }
        }
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        for (byte b : bytes) {
            builder.append(String.format("%02X", b));
        }
        return builder.toString();
    }

    //连接读卡器
    public void connectCardReader(SerialPort serialPort)
    {
        byte[] sendArray = new byte[6];
        byte tempD = 0;
        sendArray[0] = 6;
        sendArray[1] = 1;
        sendArray[2] = 65;
        sendArray[3] = 0;
        for (int i = 0; i < 4; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;
        byte[] a = new byte[1];
        a[0] = tempD;

//        System.out.println(bytesToHexString(a));

        sendArray[4] = tempD;
        sendArray[5] = 3;

        sendToPort(serialPort,sendArray);

//        System.out.println("已发送");
        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
        ComPortServiceImpl.comStateMap.put(serialPort,1);
    }

    //读取卡号
    public void readCardNum(SerialPort serialPort)
    {
        byte[] sendArray = new byte[7];
        byte tempD = 0;
        sendArray[0] = 7;
        sendArray[1] = 1;
        sendArray[2] = 68;
        sendArray[3] = 1;
        sendArray[4] = 0;
        for (int i = 0; i < 5; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;

        sendArray[5] = tempD;
        byte[] a = new byte[1];
        a[0] = tempD;
//        System.out.println(bytesToHexString(a));
        sendArray[6] = 3;

        sendToPort(serialPort,sendArray);

        ComPortServiceImpl.comStateMap.put(serialPort,2);

        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
    }






    /**
     * 从串口读取数据
     *
     * @param serialPort 当前已建立连接的SerialPort对象
     * @return 读取到的数据
    //     * @throws ReadDataFromSerialPortFailure     从串口读取数据时出错
    //     * @throws SerialPortInputStreamCloseFailure 关闭串口对象输入流出错
     */
    public  byte[] readFromPort(SerialPort serialPort)  {


        byte[] bytes = null;

        try {
            in = serialPort.getInputStream();
            int bufflenth = in.available();        //获取buffer里的数据长度

            while (bufflenth != 0) {
                bytes = new byte[bufflenth];    //初始化byte数组为buffer中数据的长度
                in.read(bytes);
                bufflenth = in.available();
            }
        } catch (IOException e) {
            e.printStackTrace();
//            throw new ReadDataFromSerialPortFailure();
        } finally {
            try {
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
//                throw new SerialPortInputStreamCloseFailure();
            }
        }
        return bytes;

    }

    //返回串口读取的数据并处理
    public String serialPort_DataReceived(SerialPort serialPort) throws InterruptedException {
        Thread.sleep(200);
        byte[] ch = readFromPort(serialPort);
        readLen = ch.length;

        //连接读卡器
        if (readLen == 26 && ch[0] == 26 && ch[1] == 1 && ch[2] == 0 && ch[3] == 20 && ch[4] == 90 && ch[5] == 76 && ch[6] == 71 && ch[25] == 3) {
//            System.out.println("读卡器连接成功");
            byte tempD = 0;
            for (int i = 0; i < 24; i++) {
                tempD = (byte) (tempD ^ ch[i]);
            }
            tempD = (byte) ~tempD;
            if (tempD == ch[24]) {
                ComPortServiceImpl.comReceiveMap.put(serialPort,true);
                return "读卡器连接成功";
            }
            return "读卡器连接失败";
        }
        //读取卡号返回的信息
        else if (readLen == 14 && ch[0] == 14 && ch[1] == 2 && ch[2] == 0 && ch[3] == 8 && ch[13] == 3) {
            byte tempD = 0;
            byte[] ICCardNum = new byte[4];
            for (int i = 0; i < 12; i++) {
                tempD = (byte) (tempD ^ ch[i]);
            }
            tempD = (byte) ~tempD;
            if (tempD == ch[12]) {
                byte[] ICNumber = new byte[4];
                ICCardNum[0] = ch[8];
                ICCardNum[1] = ch[9];
                ICCardNum[2] = ch[10];
                ICCardNum[3] = ch[11];
                ComPortServiceImpl.ICCardMap.put(serialPort,ICCardNum);

                ICNumber[0] = ch[11];
                ICNumber[1] = ch[10];
                ICNumber[2] = ch[9];
                ICNumber[3] = ch[8];
                String cardNumber = bytesToHexString(ICNumber);
                ComPortServiceImpl.comReceiveMap.put(serialPort,true);
                String message = "卡号为：" + cardNumber;
                return message;
            }
            return "读取卡号失败";
        }
        //充值、密钥验证结果
        else if (readLen == 6 && ch[0] == 6 && ch[1] == 2 && ch[5] == 3)
        {
            byte tempD = 0;
            for (int i = 0; i < 4; i++)
            {
                tempD = (byte)(tempD ^ ch[i]);
            }
            tempD = (byte)~tempD;
            if (tempD == ch[4])
            {
                if (ch[2] == 0 && ComPortServiceImpl.comStateMap.get(serialPort) == 5) {
                    ComPortServiceImpl.comReceiveMap.put(serialPort, true);
                    sendReadMifare(serialPort);
                }
                else if(ch[2] == 0 && ComPortServiceImpl.comStateMap.get(serialPort) == 7){
                    ComPortServiceImpl.comReceiveMap.put(serialPort, true);
                    return "充值成功";
                }
                else if(ch[2] != 0 && ComPortServiceImpl.comStateMap.get(serialPort) == 5){
                    return "密钥验证失败";
                }
                else if(ch[2] != 0 && ComPortServiceImpl.comStateMap.get(serialPort) == 7){
                    return "充值失败";
                }
            }
        }
        //卡激活、卡充值
        else if (readLen == 6 && ch[0] == 6 && ch[1] == 1 && ch[5] == 3) {
            byte tempD = 0;
            for (int i = 0; i < 4; i++) {
                tempD = (byte) (tempD ^ ch[i]);
            }
            tempD = (byte) ~tempD;
            if (tempD == ch[4]) {
                if (ch[2] == 0) {
                    ComPortServiceImpl.comReceiveMap.put(serialPort,true);
                    if (ComPortServiceImpl.comStateMap.get(serialPort) == 4) {
                        sendKeyYZ(serialPort,ComPortServiceImpl.ICCardMap.get(serialPort));
                    }
                    else if (ComPortServiceImpl.comStateMap.get(serialPort) == 2){
                        sendICCardActivate(serialPort);
                    }
                }
            }
        }
        //接收读取IC卡结果
        else if (readLen == 22 && ch[0] == 22 && ch[1] == 2 && ch[2] == 0 && ch[3] == 16 && ch[21] == 3)
        {
//            System.out.println("收到的原始数据是: ");
//            for (byte b : ch) {
//                System.out.print(String.format("%02X ", b & 0xFF));
//            }
            byte tempD = 0;
            for (int i = 0; i < 20; i++)
            {
                tempD = (byte)(tempD ^ ch[i]);
            }
            tempD = (byte)~tempD;
            if (tempD == ch[20])
            {
                ComPortServiceImpl.comReceiveMap.put(serialPort,true);
                sendWriteMifare(serialPort,ComPortServiceImpl.rechargeMap.get(serialPort));
                return "读取IC卡成功";
            }
            return "读取IC卡失败";
        }
        return null;
    }

//    private String timer(SerialPort serialPort) {
//        switch(ComPortServiceImpl.comStateMap.get(serialPort))
//        {
//            case 0:
//                //sendReadMachinInfo();
//                break;
//            case 1://连接读卡器
//                String message1;
//                if (ComPortServiceImpl.comReceiveMap.get(serialPort) == true) {
//                    message1 = "读卡器连接成功";
//                    return message1;
//                }
//                else {
//                    message1 = "读卡器连接异常！";
//                    return message1;
//                }
//            case 2://设置IC卡接口协议
//                break;
//            case 3://卡激活,读取卡号
//                String message3;
//                if (ComPortServiceImpl.comReceiveMap.get(serialPort) == true)
//                {
//                    message3 = "IC卡激活成功";
//                    return message3;
//                }
//                else{
//                    message3 = "IC卡激活失败！";
//                    return message3;
//                }
//            case 4://装载IC卡密钥
//                break;
//
//            case 5://E2密钥验证
//                String message5;
//                if (ComPortServiceImpl.comReceiveMap.get(serialPort) == false) {
//                    message5 = "IC卡密钥验证失败！";
//                    return  message5;
//                }
//                else {
//                    sendReadMifare(serialPort);
//                }
//                break;
//            case 6://Mifare卡读取
//                String message6;
//                if (ComPortServiceImpl.comReceiveMap.get(serialPort) == false) {
//                    message6 = "IC卡读取失败！";
//                    return message6;
//                }
//                break;
//            case 7:
//                String message7;
//                if (ComPortServiceImpl.comReceiveMap.get(serialPort) == false) {
//                    message7 = "IC卡充值失败！";
//                    return message7;
//                }
//                else {
//                    message7 = "IC卡充值成功！";
//                    return message7;
//                }
//            default:
//                break;
//        }
//        return null;
//    }


    /**
     * 激活IC卡
     * @param serialPort
     */
    private void sendICCardActivate(SerialPort serialPort)
        {
            byte[] sendArray = new byte[8];
            byte tempD = 0;
            sendArray[0] = 8;
            sendArray[1] = 2;
            sendArray[2] = 77;
            sendArray[3] = 2;
            sendArray[4] = 0;
            sendArray[5] = 38;
            for (int i = 0; i < 6; i++)
            {
                tempD = (byte)(tempD ^ sendArray[i]);
            }
            tempD = (byte)~tempD;

            sendArray[6] = tempD;
            sendArray[7] = 3;


            sendToPort(serialPort,sendArray);
            ComPortServiceImpl.comReceiveMap.put(serialPort,false);
            ComPortServiceImpl.comStateMap.put(serialPort,3);
        }

//密钥验证
    public void sendKeyYZ(SerialPort serialPort,byte[] ICCardNum)
    {
        byte[] sendArray = new byte[13];
        byte tempD = 0;
        sendArray[0] = (byte)0x0D;
        sendArray[1] = 2;
        sendArray[2] = (byte)0x45;
        sendArray[3] = 7;
        sendArray[4] = (byte)0x60;
        sendArray[5] = ICCardNum[0];
        sendArray[6] = ICCardNum[1];
        sendArray[7] = ICCardNum[2];
        sendArray[8] = ICCardNum[3];
        sendArray[9] = 0;
        sendArray[10] = 4;
        for (int i = 0; i < 11; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;

        sendArray[11] = tempD;
        sendArray[12] = 3;

        sendToPort(serialPort,sendArray);
        ComPortServiceImpl.comStateMap.put(serialPort,5);
        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
    }

        //充值
    public void reChargeIcCard(SerialPort serialPort,Float num)
    {
        money = num;
        byte[] sendArray = new byte[14];
        byte tempD = 0;
        sendArray[0] = 14;
        sendArray[1] = 1;
        sendArray[2] = 69;
        sendArray[3] = 8;
        sendArray[4] = 96;
        sendArray[5] = 0;
        sendArray[6] = -1;
        sendArray[7] = -1;
        sendArray[8] = -1;
        sendArray[9] = -1;
        sendArray[10] = -1;
        sendArray[11] = -1;
        for (int i = 0; i < 12; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;

        sendArray[12] = tempD;
        sendArray[13] = 3;

        sendToPort(serialPort,sendArray);
        ComPortServiceImpl.comStateMap.put(serialPort,4);
        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
    }

    private void sendReadMifare(SerialPort serialPort)
    {
        byte[] sendArray = new byte[7];
        byte tempD = 0;
        sendArray[0] = 7;
        sendArray[1] = 2;
        sendArray[2] = 71;
        sendArray[3] = 1;
        sendArray[4] = 4;
        sendArray[5] = 38;
        for (int i = 0; i < 5; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;
        sendArray[5] = tempD;
        sendArray[6] = 3;

        sendToPort(serialPort,sendArray);

        ComPortServiceImpl.comStateMap.put(serialPort,6);
        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
    }


    private void sendWriteMifare(SerialPort serialPort,Float num)
    {
        byte tempD = 0;
        byte[] czData = new byte[12];
        byte[] k = floatToByteArray(num);
        byte[] sendArray = new byte[23];
        sendArray[0] = (byte)0x17;
        sendArray[1] = 2;
        sendArray[2] = (byte)0x48;
        sendArray[3] = (byte)0x11;
        sendArray[4] = 4;

        byte s0, s1;
        Random rd = new Random();

        s0 = (byte)rd.nextInt(127);
        s1 = (byte)rd.nextInt(127);
        //s0 = 0x28;
        //s1 = 0x28;
        sendArray[5] = s0;
        sendArray[6] = s1;
        //取随机值的中间6位，再余24

//        (byte)((32 % 128 - (32 % 128 % 2)) / 2 % 24);

        s0 = (byte)((s0 % 128 - (s0 % 128 % 2)) / 2 % 24);
        s1 = (byte)((s1 % 128 - (s1 % 128 % 2)) / 2 % 24);
        //byte mm = Get24JY(s0 * 4 + 3);
        sendArray[7 + Get24JY(s0 * 4 + 3)] = k[0];
        //mm = Get24JY(s0 * 4 + 2);
        sendArray[7 + Get24JY(s0 * 4 + 2)] = k[1];
        //mm = Get24JY(s0 * 4 + 1);
        sendArray[7 + Get24JY(s0 * 4 + 1)] = k[2];
        //mm = Get24JY(s0 * 4 + 0);
        sendArray[7 + Get24JY(s0 * 4)] = k[3];
        //特征字符
        k[0] = (byte)0x43;// 67;
        k[1] = (byte)0x5A;//90;
        k[2] = (byte)0x6A;// 106;
        k[3] = (byte)0x65;// 101;
        sendArray[11 + Get24JY(s1 * 4)] = k[0];
        sendArray[11 + Get24JY(s1 * 4 + 1)] = k[1];
        sendArray[11 + Get24JY(s1 * 4 + 2)] = k[2];
        sendArray[11 + Get24JY(s1 * 4 + 3)] = k[3];
        byte[] crcJY = new byte[10];
        for (int i = 5; i < 15; i++)
        {
            crcJY[i - 5] = sendArray[i];
        }
        sendArray[15] = CRC16low(crcJY);
        sendArray[16] = CRC16High(crcJY);
        sendArray[17] = 0;
        sendArray[18] = 0;
        sendArray[19] = 0;
        sendArray[20] = 0;
        byte[] MD = new byte[4];
        //int MM = Convert.ToInt32(tbMM.Text);
        int MM = 0;
        MD[3] = (byte)0x55;//85;
        MD[2] = (byte)0xAA;// 170;
        MD[1] = (byte)0xCC;// 204;
        MD[0] = (byte)0x33;// 51;
        for (int i = 0; i < 4; i++)
        {
            byte td, tm;
            td = MD[i];
            tm = (byte)(MM % 256);
            MD[i] = (byte)(MD[i] ^ (MM % 256));
            MM = MM / 256;
        }

        int j = 0;
        for (int i = 5; i < 17; i++)
        {
            sendArray[i] = (byte)(sendArray[i] ^ MD[j]);
            j++;
            j = j % 4;
        }
        for (int i = 0; i < 21; i++)
        {
            tempD = (byte)(tempD ^ sendArray[i]);
        }
        tempD = (byte)~tempD;
        sendArray[21] = tempD;
        sendArray[22] = 3;

        sendToPort(serialPort,sendArray);
        ComPortServiceImpl.comStateMap.put(serialPort,7);
        ComPortServiceImpl.comReceiveMap.put(serialPort,false);
    }
    private byte Get24JY(int Ind)
    {
        int[] Ichose0 = new int[]{0x00,0x01,0x02,0x03,0x00,0x01,0x03,0x02,
                0x00,0x02,0x03,0x01,0x00,0x02,0x01,0x03,
                0x00,0x03,0x01,0x02,0x00,0x03,0x02,0x01,
                0x01,0x02,0x00,0x03,0x01,0x02,0x03,0x00,
                0x01,0x00,0x03,0x02,0x01,0x00,0x02,0x03,
                0x01,0x03,0x02,0x00,0x01,0x03,0x00,0x02,
                0x02,0x01,0x00,0x03,0x02,0x01,0x03,0x00,
                0x02,0x00,0x03,0x01,0x02,0x00,0x01,0x03,
                0x02,0x03,0x01,0x00,0x02,0x03,0x00,0x01,
                0x03,0x01,0x00,0x02,0x03,0x01,0x02,0x00,
                0x03,0x02,0x01,0x00,0x03,0x02,0x00,0x01,
                0x03,0x00,0x01,0x02,0x03,0x00,0x02,0x01,
        };
        byte[] Ichose = intToByte(Ichose0);
        return Ichose[Ind];
    }


    private byte CRC16low(byte[] data)
    {
        byte CRC16Hi, CRC16Lo;
        int i;
        int iIndex;
        CRC16Hi = (byte)0xFF;
        CRC16Lo = (byte)0xFF;


        // 遍历数据数组
        for (i = 0; i < data.length; i++)
        {
            iIndex = (CRC16Lo & 0xFF) ^ (data[i] & 0xFF);
            int unsignedResult = iIndex & 0xFF;
            CRC16Lo = (byte)((CRC16Hi & 0xFF) ^ (GetCRCLo(unsignedResult) & 0xFF) );
            CRC16Hi = GetCRCHi(unsignedResult);
        }

        return CRC16Lo;
    }
    private byte CRC16High(byte[] data)
    {
        byte CRC16Hi, CRC16Lo;
        int i;
        int iIndex;
        CRC16Hi = (byte)0xFF;
        CRC16Lo = (byte)0xFF;
        // 遍历数据数组
        for (i = 0; i < data.length; i++)
        {
            iIndex = (CRC16Lo & 0xFF) ^ (data[i] & 0xFF);
            int unsignedResult = iIndex & 0xFF;
            CRC16Lo = (byte)((CRC16Hi & 0xFF) ^ (GetCRCLo(unsignedResult) & 0xFF));
            CRC16Hi = GetCRCHi(unsignedResult);
        }
        return CRC16Hi;
    }
    private byte GetCRCLo(int Ind)
    {
        int[] Ichoose0 = new int[]{0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80,0x41, 0x0,
                0xC1, 0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1,
                0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81,
                0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40,
                0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0,
                0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1,
                0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81,
                0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41,0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40,
                0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1,
                0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x0, 0xC1,
                0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40, 0x1, 0xC0, 0x80, 0x41, 0x1, 0xC0, 0x80, 0x41, 0x0, 0xC1, 0x81, 0x40};
        byte[] Ichoose = intToByte(Ichoose0);
        return Ichoose[Ind] ;
    }
    private byte GetCRCHi(int Ind)
    {

        int[] Ichose0 = new int[] {0x0, 0xC0, 0xC1, 0x1, 0xC3, 0x3, 0x2, 0xC2, 0xC6, 0x6, 0x7, 0xC7, 0x5, 0xC5, 0xC4, 0x4, 0xCC, 0xC, 0xD, 0xCD, 0xF, 0xCF,
                0xCE, 0xE, 0xA, 0xCA, 0xCB, 0xB, 0xC9, 0x9, 0x8, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C,
                0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2,
                0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8,
                0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6,
                0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64,
                0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A,
                186, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70,
                0xB0, 0x50, 0x90,0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E,
                0x5E, 0x5A, 0x9A,0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C,
                0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 };
        byte[] Ichose = intToByte(Ichose0);

        return Ichose[Ind];
    }

    private byte[] intToByte(int[] array){
        byte[] res =  new byte[array.length];
        for(int i =0;i<array.length;i++){
            res[i] = (byte)array[i];
        }
        return  res;
    }

    private  byte[] floatToByteArray(float value) {
        ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES).order(ByteOrder.LITTLE_ENDIAN);
        buffer.putFloat(value);
        return buffer.array();
    }

        /**
         * 添加监听器
         *
         * @param port     串口对象
         * @param listener 串口监听器
        //     * @throws TooManyListeners 监听类对象过多
         */
    public  void addListener(SerialPort port, SerialPortEventListener listener)  {

        try {
            //给串口添加监听器
            port.addEventListener(listener);
            //设置当有数据到达时唤醒监听接收线程
            port.notifyOnDataAvailable(true);
            //设置当通信中断时唤醒中断线程
            port.notifyOnBreakInterrupt(true);
        } catch (TooManyListenersException e) {
//            throw new TooManyListeners();
            logger.error("太多监听器");
            e.printStackTrace();
        }
    }

        /**
         * 删除监听器
         *
         * @param port     串口对象
         * @param listener 串口监听器
        //     * @throws TooManyListeners 监听类对象过多
         */
    public  void removeListener(SerialPort port, SerialPortEventListener listener) {
        //删除串口监听器
        port.removeEventListener();
    }


}
